package cn.itcast.controller;



import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Menu;
import cn.itcast.pojo.Permission;
import cn.itcast.service.MenuService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.sound.midi.Soundbank;
import java.util.List;

@RestController
@RequestMapping(path = "/menu")
public class MenuController {

    @Reference
    private MenuService menuService;
    /*菜单回显*/
    @GetMapping("/page")
    public Result menuPage(QueryPageBean queryPageBean){
        Result Result= menuService.menuPage(queryPageBean);
        return Result;
    }
    /*
    * 新增菜单的
    * */
    @PostMapping
    public Result handleAdd(@RequestParam("permissionIds")String permissionIds, @RequestBody Menu menu){
        Integer id=null;
        if (StringUtils.isEmpty(permissionIds)){
             id = menuService.handAdd(menu);
        }else {
            id = menuService.handAdd(menu);
            System.out.println(id);
            menuService.addPremission(id,permissionIds);
        }
        return  new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }
    /**
     *    新增子菜单权限信息回显
     */

    /**
     * 删除功能
     **/
    @DeleteMapping("/{id}")
    public Result deletById(@PathVariable("id") Integer id){
        menuService.deletById(id);
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
    }
    /**
     * 增加子菜单同时增加菜单权限
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id")Integer id){
        Menu menu =menuService.findMenuById(id);
        return new Result(true,MessageConstant.DELETE_MENU_SUCCESS,menu);
    }


    @GetMapping("/{id}/permissions")
    public Result menuPermissions(@PathVariable("id")Integer id){
        Result result=menuService.menuPermissions(id);
        System.out.println("result = " + result);
        return result;
    }

    /**
     * 编辑标签的确定
     */
    @PutMapping
    public Result handleEdit(@RequestParam("permissionIds")String permissionIds , @RequestBody Menu menu){

        System.out.println(menu.getId());
        menuService.UpdateMenu(menu);
        menuService.deletPermission(menu.getId());
        menuService.addPremission(menu.getId(),permissionIds);
        return new Result(true,MessageConstant. ADD_MENU_SUCCESS);
    }
}
